package com.xiaobao.coalproperty.module.user.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class ReBindingPhoneActivity extends BaseActivity {

    @BindView(R.id.binding_phone_phone)
    EditText edtPhone;
    @BindView(R.id.binding_phone_verification)
    EditText edtVerification;
    @BindView(R.id.binding_phone_submit)
    Button btnSubmit;

    private int BINDING_TYPE = 0;

    @Override
    protected int getContentView() {
        return R.layout.activity_rebinding_phone;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        BINDING_TYPE = getIntent().getIntExtra(getIntent().getAction(), 0);
        setTitle("重新绑定手机号(1/2)");
        setLeftClickFinish();
        if (BINDING_TYPE == 1) {
            setTitle("重新绑定手机号(2/2)");
            edtPhone.setHint("请输入新手机号");
            btnSubmit.setText("完成");
        }
    }


    public void onReBindClick(View view) {
        if (!isMobileNO(edtPhone.getText().toString())) {
            showToast("手机号码格式不正确");
            return;
        }
        sendCode();
    }

    public void onReBindSubmitClick(View view) {
        submit();
    }

    private void submit() {
        OkHttpUtils.post().url(BINDING_TYPE == 0 ? UrlConstant.URL_MOBILE_BIND_FIRST : UrlConstant.URL_MOBILE_BIND_SECOND)
                .params(HttpHelperUtils.getInstance().mobileBindFirstParams(edtPhone.getText().toString(), edtVerification.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if (entity.getCode() == 0) {
                        if (BINDING_TYPE == 0) {
                            Intent intent = new Intent(ReBindingPhoneActivity.this, ReBindingPhoneActivity.class);
                            intent.setAction("type");
                            intent.putExtra("type", 1);
                            startActivity(intent);
                        }
                        finish();
                        return;
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    private void sendCode() {
        OkHttpUtils.post().url(UrlConstant.URL_SENDCODE)
                .params(HttpHelperUtils.getInstance().sendCodeParams(edtPhone.getText().toString(), "1"))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    if (entity.getCode() == 0) {
                        showToast("短信已发送，请注意查收");
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }


    public boolean isMobileNO(String mobileNums) {

        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }

}
